package com.ruoyi.yzchu.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.yzchu.domain.YzcOrderItem;
import com.ruoyi.yzchu.mapper.YzcOrderMapper;
import com.ruoyi.yzchu.domain.YzcOrder;
import com.ruoyi.yzchu.service.IYzcOrderService;

/**
 * 订单管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-06
 */
@Service
public class YzcOrderServiceImpl implements IYzcOrderService 
{
    @Autowired
    private YzcOrderMapper yzcOrderMapper;

    /**
     * 查询订单管理
     * 
     * @param orderId 订单管理主键
     * @return 订单管理
     */
    @Override
    public YzcOrder selectYzcOrderByOrderId(Long orderId)
    {
        return yzcOrderMapper.selectYzcOrderByOrderId(orderId);
    }

    /**
     * 查询订单管理列表
     * 
     * @param yzcOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<YzcOrder> selectYzcOrderList(YzcOrder yzcOrder)
    {
        return yzcOrderMapper.selectYzcOrderList(yzcOrder);
    }

    /**
     * 新增订单管理
     * 
     * @param yzcOrder 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertYzcOrder(YzcOrder yzcOrder)
    {
        yzcOrder.setCreateTime(DateUtils.getNowDate());
        int rows = yzcOrderMapper.insertYzcOrder(yzcOrder);
        insertYzcOrderItem(yzcOrder);
        return rows;
    }

    /**
     * 修改订单管理
     * 
     * @param yzcOrder 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateYzcOrder(YzcOrder yzcOrder)
    {
        yzcOrderMapper.deleteYzcOrderItemByOrderId(yzcOrder.getOrderId());
        insertYzcOrderItem(yzcOrder);
        return yzcOrderMapper.updateYzcOrder(yzcOrder);
    }

    /**
     * 批量删除订单管理
     * 
     * @param orderIds 需要删除的订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYzcOrderByOrderIds(Long[] orderIds)
    {
        yzcOrderMapper.deleteYzcOrderItemByOrderIds(orderIds);
        return yzcOrderMapper.deleteYzcOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单管理信息
     * 
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYzcOrderByOrderId(Long orderId)
    {
        yzcOrderMapper.deleteYzcOrderItemByOrderId(orderId);
        return yzcOrderMapper.deleteYzcOrderByOrderId(orderId);
    }

    /**
     * 新增订单明细（从）信息
     * 
     * @param yzcOrder 订单管理对象
     */
    public void insertYzcOrderItem(YzcOrder yzcOrder)
    {
        List<YzcOrderItem> yzcOrderItemList = yzcOrder.getYzcOrderItemList();
        Long orderId = yzcOrder.getOrderId();
        if (StringUtils.isNotNull(yzcOrderItemList))
        {
            List<YzcOrderItem> list = new ArrayList<YzcOrderItem>();
            for (YzcOrderItem yzcOrderItem : yzcOrderItemList)
            {
                yzcOrderItem.setOrderId(orderId);
                list.add(yzcOrderItem);
            }
            if (list.size() > 0)
            {
                yzcOrderMapper.batchYzcOrderItem(list);
            }
        }
    }
}
